/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 *
 */
package rad.framework.jaxws;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

/**
 * This is a base jaxws handler with common methods
 * 
 * @author ayalamanchili
 * @author boudyacho
 */
public abstract class BaseHandler implements SOAPHandler<SOAPMessageContext> {

	private Set<QName> headers = new HashSet<QName>();

	private String handlerName;

	public Set<QName> getHeaders() {
		return headers;
	}

	public void setHeaders(Set<QName> headers) {
		this.headers = headers;
	}

	public String getHandlerName() {
		return handlerName;
	}

	public void setHandlerName(String handlerName) {
		this.handlerName = handlerName;
	}

	public boolean handleMessage(SOAPMessageContext msgContext) {
		return isOutboundMessage(msgContext) ? handleOutbound(msgContext)
				: handleInbound(msgContext);
	}

	protected boolean handleInbound(SOAPMessageContext msgContext) {
		return true;
	}

	protected boolean handleOutbound(SOAPMessageContext msgContext) {
		return true;
	}

	public boolean handleFault(SOAPMessageContext smc) {
		return true;
	}

	public void close(MessageContext mc) {
	}

	public Boolean isOutboundMessage(SOAPMessageContext smc) {
		Boolean outboundProperty = (Boolean) smc
				.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
		if (outboundProperty.booleanValue()) {
			return true;
		} else {
			return false;
		}
	}

	public Boolean isInboundMessage(SOAPMessageContext smc) {
		Boolean outboundProperty = (Boolean) smc
				.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
		if (!outboundProperty.booleanValue()) {
			return true;
		} else {
			return false;
		}
	}

	protected void addHeader(SOAPMessageContext smc, QName name, String value)
			throws SOAPException {
		SOAPHeader header = smc.getMessage().getSOAPHeader();
		if (header != null) {
			SOAPElement element = header.addChildElement(name);
			element.addTextNode(value);
			smc.getMessage().saveChanges();
		}
	}

	protected String getHeaderValue(SOAPMessageContext smc, QName name)
			throws SOAPException {
		SOAPHeader header = smc.getMessage().getSOAPHeader();

		Iterator iter = header.getChildElements(name);
		if (iter.hasNext()) {
			SOAPElement element = (SOAPElement) iter.next();
			return element.getFirstChild().getNodeValue();
		}

		return null;
	}

	@Override
	public String toString() {
		return (handlerName != null ? handlerName : super.toString());
	}
}
